package com.example.jbmsuppliers.sinopec.orderaddress;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.activity.ActivityUse;
import com.example.jbmsuppliers.sinopec.adapter.UserAddressAdapter;
import com.example.jbmsuppliers.sinopec.net.NetAddRess;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.utlis.UserAddress;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.jbm.jbmsupplier.util.ICertAddress;
import com.jbm.jbmsupplier.util.ICertAddressCtrl;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class ManageAddressActivity extends ActivityUse {
    ArrayList<UserAddress> mDataList = new ArrayList<UserAddress>();
    @InjectView(R.id.id_address_manage_list)
    ListView idAddressManageList;
    @InjectView(R.id.id_address_manage_add)
    RelativeLayout idAddressManageAdd;
    @InjectView(R.id.header_back)
    ImageView headerBack;
    @InjectView(R.id.header_title)
    TextView headerTitle;
    @InjectView(R.id.header_right)
    TextView headerRight;
    private UserAddressAdapter addressAdapter = null;
    boolean isInit = false;
    private final int RESULT = 0110;
    private final int ADDRESSRESSLIST = 0001;
    private final int ADDRESSLISTSHOW = 0002;
    private final int EXIT = 0003;
    DialogLoading dialogLoading;

    @Override
    protected int setContentView1() {
        return R.layout.activity_manage_address;
    }

    @Override
    protected void initView() {

        try {
            ExitAppUtlis.getInstance().addActivity(this);
            ButterKnife.inject(this);
            headerTitle.setText("地址信息");
            headerRight.setVisibility(View.GONE);
            handler.sendMessage(handler.obtainMessage(ADDRESSRESSLIST));
            dialogLoading = new DialogLoading(this, true);
            dialogLoading.setTextstr("加载中...");

            if (idAddressManageList != null)
                idAddressManageList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        UserAddress item = mDataList.get(position);
                        Intent intent = new Intent();
                        intent.putExtra("data", item);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                });
        } catch (Exception e) {
            String msg = e.getMessage();
        }
    }

    private void AddAddress() {
        //跳转添加地址页面
        Intent intent = new Intent();
        intent.setClass(ManageAddressActivity.this, AddressEditActivity.class);
        intent.putExtra("add", "");
        startActivityForResult(intent, RESULT);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK)
            return;
        switch (requestCode) {
            case RESULT:
                int count = addressAdapter.mAddressList.size();
                for (int i = 0; i < count; i++) {
                    UserAddress address = addressAdapter.mAddressList.get(i);
                    address.isSelect = false;
                }
                ICertAddress address = (ICertAddress) data.getSerializableExtra("address");
                UserAddress newAddress = new UserAddress();
                newAddress.isSelect = true;
                newAddress.id = address.caddr_id;
                newAddress.mAddress = address.gi_text;
                newAddress.mContacts = address.contacts;
                newAddress.mMobile = address.mobile;
                addressAdapter.mAddressList.add(newAddress);
                addressAdapter.notifyDataSetChanged();
                break;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case ADDRESSRESSLIST: {
                    addressAdapter = new UserAddressAdapter(ManageAddressActivity.this, mDataList);
                    idAddressManageList.setAdapter(addressAdapter);
                    if (!isInit) {
                        NetAddRess netAddRess = new NetAddRess(handler);
                        netAddRess.AddressMessage(ADDRESSLISTSHOW);
                        dialogLoading.show();
                        isInit = true;
                    }
                    break;
                }
                case ADDRESSLISTSHOW: {
                    ICertAddressCtrl ctrl = (ICertAddressCtrl) msg.obj;
                    if (ctrl.back) {
                        if (dialogLoading != null) {
                            dialogLoading.setTextstr("加载成功...");
                            handler.sendEmptyMessageDelayed(EXIT, 2000);
                        }
                        for (int i = 0; i < ctrl.count; i++) {
                            ICertAddress key = ctrl.IndexofList(i);
                            if (key.cert_status.equals("uncert")) {
                                if (key.gi_id == 0) {
                                    ICertAddress item = ctrl.CAlist[i];
                                    UserAddress address = new UserAddress();
                                    address.mAddress = item.gi_text;
                                    address.id = item.caddr_id;
                                    address.mContacts = item.contacts;
                                    address.mMobile = item.mobile;
                                    addressAdapter.mAddressList.add(address);
                                    addressAdapter.notifyDataSetChanged();
                                }
                            }
                        }
                    } else {
                        if (dialogLoading != null) {
                            dialogLoading.setTextstr(ctrl.errorinfo);
                            handler.sendEmptyMessageDelayed(EXIT, 2000);
                        }

                    }

                    break;
                }
                case EXIT: {
                    if (dialogLoading != null) {
                        dialogLoading.dismiss();
                    }
                    break;
                }

            }


            super.handleMessage(msg);
        }
    };


    @OnClick({R.id.id_address_manage_add,R.id.header_back})
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.id_address_manage_add:
                AddAddress();
                break;
            case R.id.header_back:
                finish();
                break;
        }

    }



}
